package cn.wolfcode.luowowo.controller;


import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.exception.LogicException;
import cn.wolfcode.luowowo.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.luowowo.redis.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.redis.util.RedisKeys;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.JsonResult;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/users")
public class UserInfoController {

    @Value("${sms.url}")
    private String ssmUrl;
    @Value("${sms.logo}")
    private String ssmLogo;
    @Value("${sms.appKey}")
    private String ssmAppKey;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;
    @GetMapping("/get")
    public Object get(String id){
        return userInfoService.get(id);
    }

    @GetMapping("/checkPhone")
    public Object checkPhone(String phone){
        boolean phone1 = userInfoService.checkPhone(phone);
        return !phone1;
    }

    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone,ssmUrl,ssmLogo,ssmAppKey);
        return  JsonResult.success();
    }

    @PostMapping("/regist")
    public Object regist(String phone,String nickname,String password,String rpassword,String verifyCode){
         JsonResult result = null;

        try {
            userInfoService.regist(phone,nickname,password,rpassword,verifyCode);
            result = JsonResult.success();
        }catch (LogicException e){
            e.printStackTrace();
            result = new JsonResult(JsonResult.CODE_ERROR_PARAM,e.getMessage(),null);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult(JsonResult.CODE_ERROR_PARAM,"系统异常，请联系管理员",null);
        }
        return result;

    }
    @PostMapping("/login")
    public Object login(String username,String password){
        UserInfo user = userInfoService.login(username, password);
        String userStr = JSON.toJSONString(user);
        String token = userInfoService.setToken(userStr);
        Map map = new HashMap();
        map.put("user",user);
        map.put("token",token);
        return  JsonResult.success(map);
    }
    @RequireLogin
    @GetMapping("/getCurrentUser")
    public Object getCurrentUser(HttpServletRequest req){
        //根据请求头获取Token
        String token = req.getHeader("token");
        //根据TONKRN查找用户
        UserInfo user = userInfoService.getUserByToken(token);
        return JsonResult.success(user);
    }


    @GetMapping("/strategies/favor")
    public Object statisVo(String sid, String userId){
        List<String> list = Collections.emptyList();
        if(StringUtils.hasLength(userId)){
            list =strategyStatisVORedisService.getSids(userId);
        }
        return JsonResult.success(list);
    }
}
